var SearchObj = new Object();
SearchObj = function () {
    this.dataIndex = 0;
    this.i = 0;
    this.cookiesCount = 6;
    this.click2 = function () {
        $('.miniPreview0').remove();
        $('.miniPreview1').remove();
        term = $('#suchfeld').val();
        if (term) {
            dataIndex = 0;
            printContent(term, 20);
        }
    }

    this.enterPressed = function (e) {
        e = e || window.event;
        if (e.keyCode === 13)
            click2()

        return false;
    }

    //printContent(20);

    this.printContent = function (term, atrybutes, numElements) {
        if (term) {
            $('#loading').css('display', 'block');
            ++this.dataIndex;
            var request = this.createRequestBody(term, this.dataIndex, numElements, '', '', atrybutes);
            this.pageMethod(request, this.printHtml, this.showError);
            $('#loading').css('display', 'none');
        }
    }



    this.createRequestBody = function (term, start, numElements, version1, version2, atrs) {
        var requestedData = { Category: term, Page: start, PageSize: numElements, Ver1: version1, Ver2: version2, Atrs: atrs };
        return requestedData
    }

    ///version
    this.printVersion = function (term) {
        if (term) {
            $('#loading').css('display', 'block');
            var request = this.createRequestBody(term, 0, 0,'','');
            this.pageVersionMethod(request, this.printHtml, this.showError);
            $('#loading').css('display', 'none');

        }
    }
    
    this.printVersion3 = function (term,ver1,ver2) {
        if (term != '') {
            $('#maincontainer').html('');
            $('#loading').css('display', 'block');
            var request = this.createRequestBody(term, 0, 0, ver1, ver2,'');
            this.pageVersionMethod(request, this.printHtml, this.showError);
            $('#loading').css('display', 'none');
        }
    }


    this.pageVersionMethod = function (params, successFn, errorFn) {

        json = JSON.stringify(params);


        $.ajax({
            url: JsConst.Article_Version_Json,
            cache: false,
            type: 'POST',
            dataType: 'json',
            data: json,
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                /* get the result and do some magic with it*/
                successFn(data, "maincontainer");
            },
            error: function (xhr, ajaxOptions, thrownError) {
                callBack(xhr.status + ":" + thrownError, self);
            }
        });

    }

    //end version
    function onMOver(i, country, name, link) {
        // clearTimeout(timer);
        //  var obj = document.getElementById('image' + i);
        //   timer = setTimeout(function () { time(obj, country, name, link, i); }, 1000);
    }

    this.printHtml = function (result, container) {

        var i = 0;
        var defaultQuantity = 1;

        $.each(result, function (key, value) {
            if (!value.Name)
                value.Name = "";
            if (!value.Country)
                value.Country = "";

            var absolutePath = JsConst.Article_Category_Json;
            // add to basket

            //var singleArticleObj = new SimpleArticle();
            //todo replace i to indentifier
            var id = value.Id;
            var priceTbxId = "Price_" + id;
            var quantityId = "Quantity_" + id;
            var singleArticleObj = "singleArticleObj_" + id;


            //
            var notEraseCookie = container == "cookiecontainer" ? "cookie" : "";

            var html = '<div itemscope itemtype="http://data-vocabulary.org/Product" id="product">';
            html += '<span itemprop="name"><a href="' + value.Url + '">' + value.Name + '</a></span>\n';

            html += '<span id="priceTop">' + value.PriceTop;
            html += '<span id="priceFloor">' + value.PriceFloor + '</span>\n';

            html += '</span>\n';

            html += '<div>\n';

            html += '<a class="pImage" title="' + value.Name + '" href="' + value.Url + '">';
            html += '<img itemprop="image" src="' + value.ThumbnailPath + '" style="width: 150px;height: 150px;"/>\n';
            html += '</a>';

            html += '<a class="pImage2" title="' + value.Name + '" href="' + value.Url + '">';
            html += '<img itemprop="image" src="' + value.Image2 + '" />\n';
            html += '</a>';

            html += '<a class="pImage3" title="' + value.Name + '" href="' + value.Url + '">';
            html += '<img itemprop="image" src="' + value.Image3 + '" />\n';
            html += '</a>';


            html += '<div class="pdesc">\n';

            html += '<span itemprop="brand">' + value.Brand + '</span>\n';
            html += '<span itemprop="description">' + value.Description + '</span>\n';
            html += '<span itemprop="category" content=' + value.Category + '>' + value.Category + '</span>\n';
            html += '<span itemprop="identifier" content=' + value.IdentifierType + ':' + value.Identifier + '">' + value.Identifier + '</span>\n';
            //html += '<span itemprop="review" itemscope itemtype="http://data-vocabulary.org/Review-aggregate">\n';
            //html += '<span itemprop="rating">5</span>, liczba opinii: <span itemprop="count">10</span>\n';
            //html += '<span>Cena:' + value.Price + '</span>\n';
            html += '</div>\n';

            html += '</div>\n';
            //todo suma na > wartosc

            html += '<span >\n';

            html += '<script type="text/javascript">\n';
            html += 'var ' + singleArticleObj + ' = new SimpleArticle();';
            html += singleArticleObj + '.Init( "' + priceTbxId + '", "' + quantityId + '","' + value.Price + '","' + id + '");';
            html += '</script>';


            html += '<input type="text" name="Quantity" class="quantity_input" value="' + defaultQuantity + '"';
            html += '  style="width: 40px;" maxlength="6" onkeypress="return ' + singleArticleObj + '.NumbersOnly(event);"';
            html += '            onkeyup="' + singleArticleObj + '.QuantityChange();" onblur="' + singleArticleObj + '.QuantityChange();"';
            html += '            id="' + quantityId + '" />';
            html += '<input id="addToBasket" type="submit" onclick="' + singleArticleObj + '.AddToBasket(this);return false;"';
            html += '</span>';
            html += '<span class="calculation">';
            html += '<span class="price">___________________</span>\n';
            html += '<span class="price" id="' + priceTbxId + '">' + value.Price + '</span>\n';
            html += '</span>';
           
            $(html).appendTo("#" + container);

            ++i;


        });

        //FullClearCookie();

        for (var j = result.length - this.cookiesCount; j < result.length; ++j) {
            if (j > 0) {
                setCookie('img' + (result.length - j), result[j].Category + '~' + result[j].EncodedName + '~' + result[j].Name.replace('\'', "&rsquo;") + '~' + result[j].ImagePath);
            }
        }
    }

    this.pageMethod = function (params, successFn, errorFn) {

        json = JSON.stringify(params);


        $.ajax({
            url: JsConst.Article_Category_Json,
            cache: false,
            type: 'POST',
            dataType: 'json',
            data: json,
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                /* get the result and do some magic with it*/
                successFn(data, "maincontainer");
            },
            error: function (xhr, ajaxOptions, thrownError) {
                callBack(xhr.status + ":" + thrownError, self);
            }
        });

    }

    this.printCookie = function (data) {
        $.each(data, function (key, value) {

            var html = '<div id="dCookie" >\n';

            html += '    <a id="aCookie" href="http://bringmirbio.de/shop/' + value.Category + '"  >\n';
            html += '        <img id="imgCookie" onload="size2(this)" src="http://bringmirbio.de/shop/Content/ArticleImages/' + value.ImagePath.split('/')[2] + '"  />\n';
            html += '    </a>\n';
            html += '</div>\n';


            $(html).appendTo("#cookiecontainer");
        });
    }
  /*  this.loadFromCookie = function () {
        var cookies = new Array();
        if (document.cookie.indexOf("img1") != -1) {
            for (var i = 0; i < this.cookiesCount; ++i) {
                var cookie = getCookie('img' + (i + 1)).split('~');
                cookies.push({ 'Category': cookie[0] + '/' + cookie[1], 'ImagePath': cookie[3] });
            }
            if (cookies)
                printCookie(cookies);
        }
        else {
            cookies.push({ 'Category': 'frische-artikel-bio-gemuese/ingwer-ca-50-g-1551', 'ImagePath': 'ololo/ololo/01_ingwer.jpg' });
            cookies.push({ 'Category': 'brotaufstrich-fruchtig/quitte-ingwer-fruchtauftstrich-beerenbauern-44100', 'ImagePath': 'ololo/ololo/BB_quitte-Inger.jpg' });
            cookies.push({ 'Category': 'brotaufstrich-herzhaft/pfeffer-ingwer-zwergenwiese-21403', 'ImagePath': 'ololo/ololo/ZWE_pfeffer_ingwer.jpg' });
            cookies.push({ 'Category': 'tee-yogi-rooibush/ingwer-zitronen-tee-golden-temple-76640', 'ImagePath': 'ololo/ololo/GOL_ingwer_zitrone.jpg' });
            cookies.push({ 'Category': 'brotaufstrich-fruchtig/frucht-auslese-orange-ingwer-allos-100888', 'ImagePath': 'ololo/ololo/ALLO_fruchtauslese_oran_ing.jpg' });
            cookies.push({ 'Category': 'frische-artikel-bio-gemuese/zutaten-fuer-frische-currypaste-mit-rezept-149', 'ImagePath': 'ololo/ololo/Zutaten_Currypaste.jpg' });
            printCookie(cookies);
        }
    }
    */

    this.size = function (obj) {
        if (obj.width > obj.height)
            $(obj).css({ 'width': '78%' });
        else
            $(obj).css({ 'height': '78%' });
    }

    this.showError = function () {
        alert('Error requesting data!');
    }


    this.size2 = function (obj) {
        if (obj.width > obj.height) {
            $(obj).css({ 'width': '140px' });
            $(obj).css({ 'margin-left': '15px' });
            $(obj).css({ 'margin-top': ((150 - obj.height) / 2) + 'px' });

        }
        else {
            $(obj).css({ 'height': '140px' });

            $(obj).css({ 'margin-left': ((170 - obj.width) / 2) + 'px' });
            $(obj).css({ 'margin-top': '5px' });
        }
    }

}